<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="激活状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择激活状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否配置外部联系人权限" prop="contactAuth">
        <el-input
          v-model="queryParams.contactAuth"
          placeholder="请输入是否配置外部联系人权限"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="syncEmployeeDefine">同步企业微信通讯录</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wework:employee:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wework:employee:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wework:employee:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['wework:employee:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="employeeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" width="280">
        <template slot-scope="scope">
          <el-avatar shape="square" :size="40" :src="scope.row.thumbAvatar"></el-avatar>
          <span style="marginRight: 10px">{{ scope.row.name }}</span>
          <i v-if="scope.row.gender=='男'" class="el-icon-male"></i>
          <i v-if="scope.row.gender=='女'" class="el-icon-female"></i>
        </template>
      </el-table-column>
      <el-table-column label="激活状态" align="center" prop="status" :formatter="wxStatusFormat" />
      <el-table-column label="外部联系人权限" align="center" prop="contactAuth" />
      <el-table-column label="发起申请数" align="center" prop="applyNums" />
      <el-table-column label="新增客户数" align="center" prop="addNums" />
      <el-table-column label="聊天数" align="center" prop="messageNums" />
      <el-table-column label="发送消息数" align="center" prop="sendMessageNums" />
      <el-table-column label="已回复聊天占比" align="center" prop="replyMessageRatio" />
      <el-table-column label="平均首次回复时长" align="center" prop="averageReply" />
      <el-table-column label="删除/拉黑客户数" align="center" prop="invalidContact" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wework:employee:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wework:employee:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listEmployee, syncEmployee,getEmployee, delEmployee, addEmployee, updateEmployee, exportEmployee } from "@/api/wework/employee";
import FileUpload from '@/components/FileUpload';
import Template from "@/views/multitenant/template";

export default {
  name: "Employee",
  components: {
    Template,
    FileUpload,
  },
  data() {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业通讯录表格数据
      employeeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wxUserId: undefined,
        corpId: undefined,
        name: undefined,
        mobile: undefined,
        position: undefined,
        gender: undefined,
        email: undefined,
        avatar: undefined,
        thumbAvatar: undefined,
        telephone: undefined,
        alias: undefined,
        extattr: undefined,
        status: undefined,
        qrCode: undefined,
        externalProfile: undefined,
        externalPosition: undefined,
        address: undefined,
        openUserId: undefined,
        wxMainDeptId: undefined,
        mainDeptId: undefined,
        logUserId: undefined,
        contactAuth: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        wxUserId: [
          { required: true, message: "wx.userId不能为空", trigger: "blur" }
        ],
        corpId: [
          { required: true, message: "所属企业corpid不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        position: [
          { required: true, message: "职位信息不能为空", trigger: "blur" }
        ],
        gender: [
          { required: true, message: "性别。0表示未定义，1表示男性，2表示女性不能为空", trigger: "blur" }
        ],
        email: [
          { required: true, message: "邮箱不能为空", trigger: "blur" }
        ],
        avatar: [
          { required: true, message: "头像url不能为空", trigger: "blur" }
        ],
        thumbAvatar: [
          { required: true, message: "头像缩略图不能为空", trigger: "blur" }
        ],
        telephone: [
          { required: true, message: "座机不能为空", trigger: "blur" }
        ],
        alias: [
          { required: true, message: "别名不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "激活状态: 1=已激活，2=已禁用，4=未激活，5=退出企业不能为空", trigger: "blur" }
        ],
        qrCode: [
          { required: true, message: "员工二维码不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "地址不能为空", trigger: "blur" }
        ],
        openUserId: [
          { required: true, message: "全局唯一id不能为空", trigger: "blur" }
        ],
        wxMainDeptId: [
          { required: true, message: "微信端主部门ID不能为空", trigger: "blur" }
        ],
        mainDeptId: [
          { required: true, message: "主部门id(mc_work_department.id)不能为空", trigger: "blur" }
        ],
        logUserId: [
          { required: true, message: "子账户ID(mc_user.id)不能为空", trigger: "blur" }
        ],
        contactAuth: [
          { required: true, message: "是否配置外部联系人权限不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("employee_wx_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询企业通讯录列表 */
    getList() {
      this.loading = true;
      listEmployee(this.queryParams).then(response => {
        this.employeeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 任务组名字典翻译
    wxStatusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        wxUserId: undefined,
        corpId: undefined,
        name: undefined,
        mobile: undefined,
        position: undefined,
        gender: undefined,
        email: undefined,
        avatar: undefined,
        thumbAvatar: undefined,
        telephone: undefined,
        alias: undefined,
        extattr: undefined,
        status: "0",
        qrCode: undefined,
        externalProfile: undefined,
        externalPosition: undefined,
        address: undefined,
        openUserId: undefined,
        wxMainDeptId: undefined,
        mainDeptId: undefined,
        logUserId: undefined,
        contactAuth: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 同步企业微信
    syncEmployeeDefine () {
      syncEmployee().then(res => {
        // this.getTableData()
        this.$message.info('后台同步中,请稍等')
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业通讯录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getEmployee(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改企业通讯录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateEmployee(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEmployee(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除企业通讯录编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delEmployee(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有企业通讯录数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportEmployee(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
